﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Xml.Serialization;

namespace ELab.Serialization.Data
{
    [XmlRoot("Users")]
    public class User : IELabSerializable
    {
        [XmlElement("ApplicationName")]
        public string ApplicationName { get; set; }

        [XmlElement("ID")]
        public Guid ID { get; set; }

        [XmlElement("Type")]
        public UserType Type { get; set; }

        [XmlElement("UserName")]
        public string UserName { get; set; }

        [XmlElement("Password")]
        public string Password { get; set; }

        [XmlElement("Email")]
        public string Email { get; set; }

        [XmlElement("Fullname")]
        public string Fullname { get; set; }

        [XmlElement("IP")]
        public string IP { get; set; }

        [XmlElement("RemoteUser")]
        public string RemoteUser { get; set; }

        [XmlElement("RemotePassword")]
        public string RemotePassword { get; set; }

        [XmlElement("Avatar")]
        public byte[] Avatar { get; set; }

        [XmlArray("Surveys"), XmlArrayItem(typeof(Survey), ElementName = "Survey")]
        public Survey[] Surveys { get; set; }

        public virtual void Serialize(IELabSerializer serializer)
        {
            serializer.WriteStringUTF8(ApplicationName);
            serializer.WriteStringUTF8(ID.ToString());
            serializer.WriteStringUTF8(Type.ToString());
            serializer.WriteStringUTF8(UserName);
            serializer.WriteStringUTF8(Password);
            serializer.WriteStringUTF8(Email);
            serializer.WriteStringUTF8(Fullname);
            serializer.WriteStringUTF8(IP);
            serializer.WriteStringUTF8(RemoteUser);
            serializer.WriteStringUTF8(RemotePassword);
            serializer.WriteByteArray(Avatar);
            serializer.WriteObjectArray(Surveys);
        }

        public virtual void Deserialize(IELabDeserializer deserializer)
        {
            this.ApplicationName = deserializer.ReadStringUTF8();
            this.ID = new Guid(deserializer.ReadStringUTF8());
            UserType userType;
            Enum.TryParse<UserType>(deserializer.ReadStringUTF8(), out userType);
            this.Type = userType;
            this.UserName = deserializer.ReadStringUTF8();
            this.Password = deserializer.ReadStringUTF8();
            this.Email = deserializer.ReadStringUTF8();
            this.Fullname = deserializer.ReadStringUTF8();
            this.IP = deserializer.ReadStringUTF8();
            this.RemoteUser = deserializer.ReadStringUTF8();
            this.RemotePassword = deserializer.ReadStringUTF8();
            this.Avatar = deserializer.ReadByteArray();
            this.Surveys = deserializer.ReadObjectArray(() => new Survey());
        }
    }
}
